package 力扣日常刷题.木23一月.第03天303;

import java.util.Arrays;

/**
 * @author 帅小伙
 * @date 2022/3/3
 * @description
 */
public class Demo26最长回文子串 {

    public static void main(String[] args) {
        longestPalindrome("cbbd");
    }


    public static String longestPalindrome(String s) {
        int n = s.length();
        boolean dp[][] = new boolean[n][n];
        for (int i = 0; i < n; i++) Arrays.fill(dp[i],true);


        int maxLen = 1,begin = 0;
        for (int i = n - 1; i >= 0 ; i--) {
            for (int j = i+1; j < n; j++) {
                dp[i][j] = (s.charAt(i) == s.charAt(j)) && dp[i+1][j - 1];
                if(dp[i][j] && j - i + 1 > maxLen) {
                    maxLen = j - i + 1;
                    begin = i;
                }
            }
        }
        return s.substring(begin,begin+ maxLen);

    }

}
